﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utilities;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class BannerController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: Banner
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Select(int page, int limit, string title, string begin, string end)
        {
             
            {
                var q = db.Banner.Where(p => true);

                if (title != "" && title != null)
                {
                    q = q.Where(p => p.title.Contains(title));
                }
                if (!string.IsNullOrWhiteSpace(begin))
                {
                    var b = Convert.ToDateTime(begin);
                    q = q.Where(x => x.create_time >= b);//开始时间
                }
                if (!string.IsNullOrWhiteSpace(end))
                {
                    var e = Convert.ToDateTime(end);
                    q = q.Where(x => x.create_time <= e);//结束时间
                }
                var count = q.Count();
                var list = q.OrderByDescending(x => x.id).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
                {
                    id = x.id,
                    bannerImg = x.bannerImg,
                    create_user = x.create_user,
                    create_time = x.create_time.ToString(),
                    title = x.title,
                    url = x.url
                });
                return Json(new { code = 0, msg = "", count = count, data = list });
            }

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();

        }

        public ActionResult DAdd(Banner banner)
        {
             
            {
                banner.create_time = DateTime.Now;
                db.Banner.Add(banner);
                var r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "成功" });
                }
                else
                {
                    return Json(new { code = 0, msg = "失败" });

                }
            }
        }
        public ActionResult Edit(int id)
        {
             
            {
                var banner = db.Banner.SingleOrDefault(p => p.id == id);
                ViewBag.A = banner;
            }
            return View();
        }
        public ActionResult DealEdit(Banner banner)
        {
             
            {
                var info = db.Banner.SingleOrDefault(p => p.id == banner.id);
                info.title = banner.title;
                info.bannerImg = banner.bannerImg;
                info.url = banner.url;
                
                info.create_user = banner.create_user;
                banner.create_time = DateTime.Now;
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "编辑失败" });
                }
            }

        }

        public ActionResult Delect(int id)
        {
             
            {
                var info = db.Banner.SingleOrDefault(p => p.id == id);
                db.Banner.Remove(info);
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "删除失败" });
                }
            }

        }

        public ActionResult ListToExcel(string title)
        {
             
            {
                //读取所有的轮播图
                var list = db.Banner.Where(p => true);

                if (title != "" && title != null)
                {
                    list = list.Where(p => p.title.Contains(title));
                }

                //把list转换成DataTable
                DataTable dt = Extensions.ToDataTable(list.ToList());
                //dt转换成二进制文件
                byte[] file = ExcelHelper.DataTableToExcel(dt, "用户信息", true);
                //导出
                return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            }
        }
    }
}
